//
//  TravelDitailedScrollerView.swift
//  GoTravel
//
//  Created by lanou3g on 16/3/11.
//  Copyright © 2016年 SHH. All rights reserved.
//

import UIKit

class TravelDetailedScrollerView: UIViewController,UIScrollViewDelegate,UIAlertViewDelegate{

    //详情界面轮播图
    var detailedScroll:UIScrollView?
    
    //page开关
    var DTPage:UIPageControl?
    
    //页面数
    var pageNumber:Int?
    
    //详情按钮
    var detailBut:UIButton?
    
    //语言框
    var detailLabel:UILabel?
    
    //展开按钮
//    var openBut:UIButton?
    
    //推出弹窗
//    var textAlert:UIAlertView?
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = UIColor.whiteColor()
        //设置此属性为否，防止图片滑动时向上偏移
        self.automaticallyAdjustsScrollViewInsets = false
        addView()
        
    
    }

    func addView() -> Void{
//轮播图
        pageNumber = 2 //测试用
        self.detailedScroll = UIScrollView()
        self.detailedScroll?.bounces = false

        self.detailedScroll?.frame = CGRectMake(0, self.view.frame.height * 0.12, self.view.frame.width, self.view.frame.height * 0.78)
        self.detailedScroll?.contentSize = CGSizeMake(self.view.frame.width * CGFloat(pageNumber!), self.view.frame.height * 0.8)
        self.detailedScroll?.pagingEnabled = true //分页开关
        self.view.addSubview(self.detailedScroll!)
        self.detailedScroll?.delegate = self  //设置ScrollView代理
        //添加图片
        for var i = 1;i < pageNumber! + 1;i++ {
            let imageName:String = "last\(i).jpg" //测试用
            let imageForSCV:UIImageView = UIImageView()
            imageForSCV.image = UIImage(named: imageName)
            
            imageForSCV.frame = CGRectMake(self.view.frame.width * CGFloat(i - 1), self.view.frame.height * 0.075, self.view.frame.width, self.view.frame.height * 0.8)
            self.detailedScroll?.addSubview(imageForSCV)
        
        }
        
//pageController
        self.DTPage = UIPageControl()
        self.DTPage?.frame = CGRectMake(self.view.frame.width * 0.5 - self.view.frame.width * 0.25 * 0.5, (self.detailedScroll?.frame.maxY)! - self.view.frame.height * 0.046, self.view.frame.width * 0.25, self.view.frame.height * 0.046)
        self.DTPage?.center.x = self.view.center.x
        self.DTPage?.backgroundColor = UIColor.clearColor()
        self.DTPage?.numberOfPages = pageNumber!
        self.DTPage?.addTarget(self, action: Selector("moveAction:"), forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addSubview(self.DTPage!)
     
//detailBut
        self.detailBut = UIButton()
        self.detailBut?.frame = CGRectMake(self.view.frame.maxX - self.view.frame.width * 0.09, (self.detailedScroll?.frame.minY)!, self.view.frame.width * 0.07, self.view.frame.width * 0.07)
//        self.detailBut?.frame = CGRectMake(100, 70, 30, 30)
        self.detailBut?.setImage(UIImage(named: "but2.jpg"), forState: UIControlState.Normal)
        self.detailBut?.setImage(UIImage(named: "but1.jpg"), forState: UIControlState.Selected)
        self.detailBut?.addTarget(self, action: Selector("clickDetail:"), forControlEvents: UIControlEvents.TouchUpInside)
        self.detailBut?.layer.masksToBounds = true
        self.detailBut?.layer.cornerRadius = 13
        self.view.addSubview(self.detailBut!)
        
//detailLabel
        self.detailLabel = UILabel()
        self.detailLabel?.frame = CGRectMake(self.view.frame.width * 0.027, (self.detailBut?.frame.minY)! - self.view.frame.width * 0.035, self.view.frame.width * 0.866, self.view.frame.height * 0.12 - self.view.frame.width * 0.054)
        self.detailLabel?.text = "今天是答辩的日子" //测试用
        self.detailLabel?.numberOfLines = 0
        self.detailLabel?.font = UIFont(name: "Chalkduster", size: 12)
        self.view.addSubview(self.detailLabel!)
    
//openBut
//        self.openBut = UIButton()
//        self.openBut?.frame = CGRectMake(self.view.frame.width * 0.826, (self.detailBut?.frame.maxY)! + 5
//            , 20, 10)
//        self.openBut?.setTitle("展开", forState: UIControlState.Normal)
//        self.openBut?.titleLabel?.font = UIFont(name: "Chalkduster", size: 10)
//        self.openBut?.addTarget(self, action: Selector("openAction:"), forControlEvents: UIControlEvents.TouchUpInside)
//        self.openBut?.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)
//        self.view.addSubview(self.openBut!)
    }
    
    
//给pageC添加事件
    func moveAction(sender:UIPageControl) -> Void{
        print(sender.currentPage)
        
        self.detailedScroll?.contentOffset = CGPointMake(CGRectGetWidth((self.detailedScroll?.frame)!) * (CGFloat)(sender.currentPage), 0)
    }

//给详情按钮添加事件
    func clickDetail(sender:UIButton) -> Void{
        
        let aTextView = TextView()
        aTextView.TString = self.detailLabel?.text
        self.presentViewController(aTextView, animated: true, completion: nil)
    }

//给展开按钮添加事件
//    func openAction(sender:UIButton) -> Void{
//    }
    
//实现pageC随scrollView滑动而变化
    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        self.DTPage?.currentPage = (NSInteger)(scrollView.contentOffset.x) / (NSInteger)(CGRectGetWidth(scrollView.frame))
     }
    
    
    

    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    
    }
    


    
    
}
